package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONArray;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.boce.task.GenericTask;
import com.shengyuan.manage_os.old.ying.boce.task.TaskAdapter;
import com.shengyuan.manage_os.old.ying.boce.task.TaskListener;
import com.shengyuan.manage_os.old.ying.boce.task.TaskParams;
import com.shengyuan.manage_os.old.ying.boce.task.TaskResult;
import com.shengyuan.manage_os.old.ying.erpm.controls.SourceSelectMenu;
import com.shengyuan.manage_os.old.ying.erpm.controls.SourceSelectMenu.OnPopClickListener;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

public class Actual_Form_XieDi extends Actual_Form_ZhuangDi {

	public final static String  T_ZHUANGDIDAN = "get_data_zhuangdidan";
	private long mTaskID;
	private EditText editText_car_xiedi, editText_zhuangditime;
	private JSONArray mJsonArray; int CarIndex;
	private String mProject, mTask;
	@Override
	public String MyID() {
		return getClass().getName();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//super.onCreate(savedInstanceState);
		onCreate_copy(savedInstanceState);
		setContentView(R.layout.actual_form_xiedi);
		InitControls();
		editText_car_xiedi = (EditText) findViewById(R.id.editText_car_xiedi);
		if (editText_car_xiedi!=null) {
			editText_car_xiedi.setOnLongClickListener(this);
			editText_car_xiedi.setInputType(InputType.TYPE_NULL);

		}
		editText_zhuangditime = (EditText) findViewById(R.id.editText_zhuangditime);
		//GetSpinnerCar2(Actual_Form_XieDi.class.getName());
	}
	
    @Override
	public boolean onLongClick(View view) {
    	super.onLongClick(view);
		switch (view.getId()) {
		case R.id.editText_car_xiedi:
			Utility.HideEditFouce(editText_car_xiedi);

			Submit_System_Setting_Data_Task mSubmitTask = new Submit_System_Setting_Data_Task();
			mSubmitTask.setListener(mTaskListener);
			TaskParams params;
			params = new TaskParams();
			params.put(Submit_System_Setting_Data_Task.TYPENAME, this.T_ZHUANGDIDAN);
			params.put(Submit_System_Setting_Data_Task.TYPECODE, LoginActivity.pmUser.UserName);
			mSubmitTask.execute(params);
			break;
		}
		return true;
	}
    
	private TaskListener mTaskListener = new TaskAdapter() {
		public ProgressDialog mDialog;
		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			if (mDialog!=null) {
				mDialog.dismiss();
			}
			try {
				Submit_System_Setting_Data_Task t = (Submit_System_Setting_Data_Task)task;
				if (result == TaskResult.OK) {
					String data = t.pJsonArray.toString();
					if (data!=null && !data.equals("") && !data.equals("[]") ){
						mJsonArray = new JSONArray(data);
						String[] zhuangdicar = new String[mJsonArray.length()];
						for (int i = 0; i < mJsonArray.length(); i++) {
							zhuangdicar[i] = mJsonArray.getJSONObject(i).getString("ChePaiHaoMa");
						}
						if (zhuangdicar!=null 
								&& zhuangdicar.length>0) {
									SourceSelectMenu menu = new SourceSelectMenu(editText_car_xiedi);
									menu.setArrays(zhuangdicar);
									menu.show();
									menu.setOnPopClickListener(new onPopClickListener_XieDi());
								}
						Utility.MessageBox(Actual_Form_XieDi.this,getString(R.string.setting_success_system_data));
					}
	
				} else {
					Utility.MessageBox(Actual_Form_XieDi.this,t.mErrorMessage);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}


		@Override
		public void onPreExecute(GenericTask task) {
			mDialog = Utility.ShowModelDialog( Actual_Form_XieDi.this, getString(R.string.setting_system_update));
			mDialog.show();
		}
	};
	
	
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
			String value = editText_car_xiedi.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_car_xiedi.getHint().toString());
				return false;
			}
			value = editText_sunhao.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_sunhao.getHint().toString());
				return false;
			}
			return true;
		}else{
			return false;
		}
	}

	
	@Override
	protected void SaveJsonData() {
		try {
			IsJson1 = true; mFinished = 1;
			JsonMyID();
			mJson.put( "装地单号:" ,  mTaskID + "");
			mJson.put( "工程项目:" ,  mProject );
			mJson.put( "任务名称:" ,  mTask );
			//JsonMyTask("装地单号:", mTaskID + "");
			//JsonMyTask("工程项目:", mProject);
			if (editText_car_xiedi!=null) {
				mJson.put( getString(R.string.title_actual_form_editText_car), editText_car_xiedi.getText().toString());
			}
			if (editText_zhuangditime!=null) {
				mJson.put( getString(R.string.title_actual_form_editText_zhuangdi_time), editText_zhuangditime.getText().toString());
			}
			getCar();
			getZhuangdi();
			getXiedi();
			getSunHao();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
		}catch (Exception e) {
		}
	}
	
	@Override
	protected void SpinnerToEditText(int index) {
		SetEditValueAndFalse(editText_chexing, index, getString(R.string.title_actual_form_editText_chexing));
		SetEditValueAndFalse(editText_cailiao, index, getString(R.string.title_actual_form_editText_cailiao));
		SetEditValueAndFalse(editText_gongyingshang, index, getString(R.string.title_actual_form_editText_gongyingshang));
		//得到网络 ID号
		mTaskID = index;
	}
	public class onPopClickListener_XieDi implements OnPopClickListener {
		@Override
		public void onPopClick(int index, String text, View anchor) {
			EditText etext = (EditText) anchor;
			if (etext!=null) {
				etext.setText(text);
			}
			try {
				CarIndex = index;
				String json1 = mJsonArray.getJSONObject(index).getString("Json1");
				mTaskID = mJsonArray.getJSONObject(index).getLong("ID");
				JSONArray array = new JSONArray("["+json1+"]");
				editText_chexing.setText( array.getJSONObject(0).getString("车辆型号:") );
				editText_cailiao.setText( array.getJSONObject(0).getString("材料名称:"));
				editText_gongyingshang.setText( array.getJSONObject(0).getString("供应商家:"));
				editText_zhuangdi.setText( array.getJSONObject(0).getString("起始装地:"));
				editText_zhuangditime.setText( array.getJSONObject(0).getString("现场时间:"));
				mProject = array.getJSONObject(0).getString("工程项目:");
				mTask = array.getJSONObject(0).getString("任务名称:");
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
}
